// pages/newquanyizhuce/newquanyizhuce.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CUSTOMER_NAME: '',
    CUSTOMER_TEL: '',
    IDCARD: '',
    fuwu: false,
    yinsi: false,
    flag: true, //设置节流
    checked: false,
    timer: "", // 定时器名字
    countDownNum: '60',
    verify: "",
    flag: true, //设置节流
    zongnone: '',
    shenfenjiami: '',



    wang_d: '', //地点
    city: [], //市
    district: [], //区
    business: '', //网点
    shi_k: true,
    qu_k: true,
    wang_k: true,
    tishi: true,



    shiquxuan: true,

    L2name: '',
    L3name: '',
    L4name: '',

    L4code: '',
  },


  daxiuxin() {
    wx.navigateTo({
      url: '../../pages/newxiugaixingxi/newxiugaixingxi',
    })
  },










  region() {
    wx.request({
      url: app.globalData.interface + '/newVersion/API_2029_outletPush',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          city: res.data.data,
        })

      }
    })
  },


  but_shi() {
    this.setData({
      shi_k: false,

    })

    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });

    console.log(this.data.session_key);
  },

  but_xian() {
    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择市',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        qu_k: false
      })
    }
  },

  but_w() {

    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择区',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        wang_k: false
      })
    }

  },


  //市
  buttn_c(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.district);
    console.log(e.currentTarget.dataset.item.L2code);
    this.setData({
      district: e.currentTarget.dataset.item.district,
      L2code: e.currentTarget.dataset.item.L2code,
      L2name: e.currentTarget.dataset.item.L2name,
      shi_k: true,
      L3name: '',
      L4name: '',

    })

  },

  buttn_x(e) {
    console.log(e);


    this.setData({
      L3code: e.currentTarget.dataset.item.L3code,
      L3name: e.currentTarget.dataset.item.L3name,
      qu_k: true,
      L4name: '',
    })
    this.Outlets()

  },


  buttn_w(e) {
    console.log(e);
    this.setData({
      L4name: e.currentTarget.dataset.item.L4name,
      L4code: e.currentTarget.dataset.item.L4code,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,

      wang_k: true
    })





  },

  //网点
  Outlets() {
    wx.request({
      url: app.globalData.interface + '/newVersion/API_2027_outletList',
      method: 'POST',
      data: {
        L2code: this.data.L2code,
        L3code: this.data.L3code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.data);
        this.setData({
          business: res.data.data
        })

      }
    })
  },




  //网点
  bouut() {
    wx.request({
      url: app.globalData.interface + '/crossVersionOne/API2_2131_UpdateUserBrch',
      method: 'POST',
      data: {

        OPEN_ID: this.data.OPEN_ID, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami,
        L4code: this.data.L4code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);

        wx.showModal({
          title: '温馨提示',
          content: res.data.data,
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 1,
              })
            } else { //这里是点击了取消以后
              console.log('用户点击取消')
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })


      }
    })
  },








  fetch: function () {
    this.setData({

      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,

    })


    if (this.data.flag) {
      this.data.flag = false
      if (this.data.CUSTOMER_TEL == '') {
        wx.showToast({
          title: '请填写手机号！',
          icon: 'none',
          duration: 3000
        })
      } else if (this.data.session_key == "" || this.data.jiami == "" || this.data.session_key == null || this.data.jiami == null || this.data.session_key == undefined || this.data.jiami == undefined) {
        wx.showModal({
          title: '温馨提示',
          content: '数据错误请返回首页',
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后   
              console.log('用户点击确定')

              wx.switchTab({
                url: '../../pages/newshouye/newshouye',
              })

            } else { //这里是点击了取消以后
              console.log('用户点击取消')
              wx.switchTab({
                url: '../../pages/newshouye/newshouye',
              })

            }
          }
        })
      } else {
        wx.request({
          url: app.globalData.interface + '/app/API_1052_sendMassage',
          method: 'GET',
          data: {
            phone: this.data.CUSTOMER_TEL,
            KEY: 'xianjgjKey9999',
            OPEN_ID: app.globalData.openid, //用户OPEN_ID
            session_key: this.data.session_key,
            OPEN_UUID: this.data.jiami,
            phone_type: ''
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {

            if (res.data.result == 'error') {
              wx.showToast({
                title: res.data.code,
                icon: 'none',
                duration: 3000
              })
            } else {
              wx.showToast({
                title: res.data.code,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
        var _this = this;
        var countDownNum = _this.data.countDownNum; // 获取倒计时初始值
        var timer = setInterval(function () {
          countDownNum -= 1;
          _this.setData({
            countDownNum: countDownNum
          })
          if (countDownNum <= -1) {
            clearInterval(timer);
            // 取消置顶的setInterval函数将要执行的代码
            _this.setData({
              countDownNum: 60,
            })
          }
        }, 1000)

      }
      setTimeout(() => {
        this.data.flag = true
      }, 5000)


    }
  },





  baoming() {

    this.setData({
      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

    var that = this

    if (that.data.flag) {
      that.data.flag = false

      if (that.data.checked == false) {
        wx.showToast({
          title: "请勾选用户协议",
          icon: "none",
          duration: 2000
        })

      } else if (that.data.verify == '' || that.data.CUSTOMER_NAME == '' || that.data.CUSTOMER_TEL == '' || that.data.IDCARD == '') {
        wx.showToast({
          title: '请完善报名信息',
          icon: 'none',
          duration: 2000
        })


        console.log(that.data.verify);
        console.log(that.data.CUSTOMER_NAME);
        console.log(that.data.CUSTOMER_TEL);
        console.log(that.data.IDCARD);
      } else {

        wx.request({
          url: app.globalData.interface + '/newVersion/API_2040_AddWeixinUser',
          method: 'POST',
          data: {
            CUSTOMER_NAME: that.data.CUSTOMER_NAME,
            CUSTOMER_TEL: that.data.CUSTOMER_TEL,
            IDCARD: that.data.IDCARD,
            OPEN_ID: app.globalData.openid, //用户OPEN_ID
            session_key: app.globalData.session_key,
            OPEN_UUID: app.globalData.jiami,
            code: that.data.verify,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {



            if (res.data.result == 'error') {

              wx.showToast({
                title: res.data.data,
                icon: 'none',
                duration: 3000
              })


            } else {


              wx.showModal({
                title: '温馨提示',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后
                    that.zhuce()
                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                    that.zhuce()
                  }
                }
              })


            }
          }
        })
      }
      setTimeout(() => {
        that.data.flag = true
      }, 3000)
    }
  },





  zhuce() {

    

    this.setData({
      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    })
    if (this.data.flag) {
      this.data.flag = false
      wx.request({
        url: app.globalData.interface + '/newVersion/API_2040_SelectWeixinUser',
        method: 'POST',
        data: {
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key: app.globalData.session_key,
          OPEN_UUID: app.globalData.jiami,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },

        success: res => {

          console.log(123);


          let that = this
          console.log(res.data.result);
          console.log(123);
          if (res.data.result == 'error') {
            wx.showModal({
              title: '温馨提示',
              content: '未注册，请先注册',
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后

                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else {

            console.log(res.data.data.externalUserId);

            if (res.data.data.externalUserId == '0') {

              that.setData({
                shiquxuan: false,
              })


            } else {
              wx.showModal({
                title: '温馨提示',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后
  
                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
            }
          }
        }
      })
      setTimeout(() => {
        this.data.flag = true
      }, 3000)
    }
  },





















  // 复选框
  fuxk(event) {
    console.log(event.detail);
    this.setData({
      checked: event.detail,
    });
  },

  fuwu() {
    // this.setData({ show: true });
    this.setData({
      fuwu: !this.data.fuwu
    })
  },

  yinsi() {
    // this.setData({ show: true });
    this.setData({
      yinsi: !this.data.yinsi
    })
  },


  back() {
    wx.navigateBack({
      delta: 1
    })
  },







  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })
    } else if (this.data.mobile.length < 11) {
      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },


  idcardblur: function (e) {
    let idcard = e.detail.value;
    let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;

    if (reg.test(idcard) == false) {
      this.setData({
        IDCARD: ''
      })
      wx.showToast({
        title: '身份证号码有误',
        icon: 'none',
        duration: 3000
      })



    } else {
      this.setData({
        IDCARD: idcard
      })


    }

  },







  search() {

    this.setData({

      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,

    })





    wx.request({
      url: app.globalData.interface + '/newVersion/API_2010_selectUserSome',
      method: 'POST',
      data: {
        CUSTOMER_ID: this.data.CUSTOMER_ID,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data);
        var shenfenjiami = res.data.data.IDCARD.substr(0, 6) + "********" + res.data.data.IDCARD.substr(14)


        this.setData({
          zongnone: res.data.data,
          CUSTOMER_NAME: res.data.data.CUSTOMER_NAME,
          CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,
          shenfenjiami: shenfenjiami,
          IDCARD: res.data.data.IDCARD,
          IDCARDNULL: res.data.data.IDCARDNULL,
        })

      }
    })
  },




























  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({

      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,

    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.region()
    this.search()

    this.zhuce()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})